package com.bardpub.controls 
{
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName BRadio.as
	 * @author LittleBard
	 * @version 1.0
	 * @date 2010/4/28
	 * @descrition 单选框组件
	 * @others
	 * @history
	 */

	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class BRadio extends BComponent
	{
		private static var radios:Array;
		
		private var __label:String;
		private var __checked:Boolean;
		private var __labField:TextField;
		private var	__groupName:String;
		private var __icon:Sprite;
		private var __selectedIcon:Sprite;
		private var __hitTest:Sprite;
		private var __txtColor:uint;
		private var __value:String;
		
		/**
		 * 创建com.bardpub.controls.BRadio类的新实例
		 * @param	parent	组件父容器
		 * @param	x		组件x坐标
		 * @param	y		组件y坐标
		 * @param	label	组件的标签文本
		 * @param	checked	选中状态
		 */
		public function BRadio(parent:DisplayObjectContainer = null, 
							   x:Number = 0, 
							   y:Number = 0, 
							   label:String = "Label",
							   checked:Boolean = false) 
		{
			registerRadio(this);
			__label = label;
			__checked = checked;
			__value = "";
			super(parent, x, y);
		}
		
		/**
		 * 为组件添加子元素
		 */
		override protected function addChildren():void 
		{
			__hitTest = new Sprite();
			__hitTest.graphics.beginFill(0, 0);
			__hitTest.graphics.drawRect(0, 0, 10, 14);
			__hitTest.graphics.endFill();
			__hitTest.buttonMode = __hitTest.useHandCursor = true;
			__hitTest.addEventListener(MouseEvent.CLICK, clickHandler);
			addChild(__hitTest);
		}
	
		/**
		 * 组件初始化
		 */
		override protected function initialize():void 
		{
			__groupName = "defaultRadioGroup";
			super.initialize();
			
			__labField = new TextField();
			__labField.autoSize = "left";
			__labField.defaultTextFormat = new TextFormat("arial");
			__labField.mouseEnabled = __labField.selectable = false;
			addChild(__labField);
			
			try 
			{
				var ref:Class = getClassByName("BRadio_Icon");
				__icon = new ref() as Sprite;
				
				ref = getClassByName("BRadio_SelectedIcon");
				__selectedIcon = new ref() as Sprite;
			}
			catch (e:*)
			{
				throw new Error("BRadio Create Skin Failed");
			}
				
			__labField.x = __icon.width + 3;
			__labField.y = -2.5;
		}
		
		/**
		 * 绘制组件UI
		 */
		override public function draw():void 
		{
			__labField.text = __label;
			__labField.textColor = __txtColor;
			
			var cur:Sprite = __checked ? __icon : __selectedIcon;
			var next:Sprite = __checked ? __selectedIcon : __icon;
			
			if (cur && cur.parent)
			{
				removeChild(cur);
			}
			if (next && next.parent == null)
			{
				addChild(next);
			}
			setChildIndex(__hitTest, this.numChildren - 1);
			__hitTest.width = __labField.width + __labField.x;
			if (__label == "") 
			{
				__hitTest.width -= 6;
			}
			
			__width = __hitTest.width;

		}
	
/*================== static methods ========================*/
		/**
		 * 将新创建的组件添加到静态数组radios中
		 * @param	instance	要添加的组件
		 */
		private static function registerRadio(instance:BRadio):void 
		{
			if (radios == null)
			{
				radios = new Array();
			}
			radios.push(instance);
		}
		
		/**
		 * 清除某组中除了指定实例以外的Radio的选中状态
		 * @param	instance	要保留转中状态的实例
		 */
		private static function clear(instance:BRadio):void 
		{
			for (var i:int = 0, len:int = radios.length; i < len; i++ ) 
			{
				var radio:BRadio = radios[i] as BRadio;
				if (radio != instance && radio.groupName == instance.groupName && radio.selected )
				{
					radio.selected = false;
				}
			}
		}
		
		
/*================= getter/setter ============================*/

		/**
		 * 设置组件的文本颜色
		 */
		[Inspectable(name = "txtColor", type = "Number", defaultValue = 0)]
		public function set txtColor(value:Number):void 
		{
			__txtColor = value;
			invalidate();
		}


		/**
		 * 设置BRadio组件的文本标签
		 */
		[Inspectable(name = "label", type = "String", defaultValue = "Label")]
		public function set label(value:String):void 
		{
			__label = value;
			invalidate();
		}
		
		/**
		 * 获取/设置BRadio组件是否选中状态
		 */
		public function get selected():Boolean 
		{
			return __checked;
		}
		[Inspectable(name = "selected", type = "Boolean", defaultValue = false)]
		public function set selected(value:Boolean):void 
		{
			if (value != __checked)
			{
				__checked = value;
				invalidate();
				if (__checked == true)
				{
					clear(this);
				}
			}
		}
		
		/**
		 * 获取/设置组件的值
		 */
		public function get value():String { return __value; }
		[Inspectable(name = "value", type = "String", defaultValue = "")]
		public function set value(v:String):void 
		{
			__value = v;
		}
		
		/**
		 * 获取/设置 Radio的GroupName
		 */
		public function get groupName():String 
		{
			return __groupName;
		}
		[Inspectable(name = "groupName", type = "String", defaultValue="defaultRadioGroup")]
		public function set groupName(value:String):void 
		{
			__groupName = value;
		}
		
		

/*================= event handlers ============================*/
		private function clickHandler(e:MouseEvent):void 
		{
			if (!__checked)
			{
				selected = !__checked;
			}
		}
		
	}

}